import request from '@/utils/request';
import type { Result, PageResult } from '@/types/global';
import axios from 'axios';

// 充电订单列表查询参数接口
export interface ChargingOrderQueryParams {
  current?: number;
  size?: number;
  orderNo?: string;
  mobile?: string;
  vehiclePlate?: string;
  orderStatus?: number;
  paymentStatus?: number;
  startTime?: string;
  endTime?: string;
  endStartTime?: string;
  endEndTime?: string;
}

// 充电订单实体接口
export interface ChargingOrder {
  id: number;
  orderNo: string;
  thirdPartyOrderNo?: string;
  thirdPileNo?: string;
  platformId?: number;
  platformName?: string;
  pileId?: number;
  gunId?: number;
  stationId?: number;
  startTime?: string;
  endTime?: string;
  chargingDuration?: number;
  startSoc?: number;
  endSoc?: number;
  chargingElec?: number;
  orderAmount?: number;
  serviceFee?: number;
  electricityFee?: number;
  parkingFee?: number;
  discountAmount?: number;
  actualAmount?: number;
  paymentMethod?: number;
  paymentTime?: string;
  paymentStatus?: number;
  transactionId?: string;
  orderStatus?: number;
  stopReason?: number;
  stopReasonDesc?: string;
  vehiclePlate?: string;
  vehicleModel?: string;
  batteryCapacity?: number;
  peakPower?: number;
  peakCurrent?: number;
  peakVoltage?: number;
  avgPower?: number;
  chargingDetails?: string;
  createTime?: string;
  updateTime?: string;
  createBy?: string;
  updateBy?: string;
  remark?: string;
  vin?: string;
  mobile?: string;
}

// 获取充电订单列表
export function getChargingOrderList(params: ChargingOrderQueryParams) {
  return request.get<{
    records: ChargingOrder[];
    total: number;
    size: number;
    current: number;
  }>('/api/order/list', params);
}

// 获取充电订单详情
export function getChargingOrderDetail(id: string | number) {
  return request.get<ChargingOrder>(`/api/order/${id}`);
}

// 支付状态枚举
export const PaymentStatusEnum = {
  UNPAID: 0,
  PAID: 1,
  REFUNDED: 2,
  PARTIAL_REFUNDED: 3,
} as const;

// 订单状态枚举
export const OrderStatusEnum = {
  WAITING: 0, // 等待中
  CHARGING: 1, // 正在充电
  FAILED: 2, // 充电失败
  COMPLETED: 3, // 充电完成
} as const;

// 支付方式枚举
export const PaymentMethodEnum = {
  WECHAT: 1,
  ALIPAY: 2,
  UNIONPAY: 3,
  BALANCE: 4,
} as const;

// 停止原因枚举
export const StopReasonEnum = {
  NORMAL: 1,
  MANUAL: 2,
  SYSTEM_ERROR: 3,
  TIMEOUT: 4,
  INSUFFICIENT_BALANCE: 5,
} as const;

// 订单统计数据接口
export interface ChargingOrderStatistics {
  orderCount: number;
  totalChargingElec: number;
  totalOrderAmount: number;
}

// 获取充电订单统计数据
export function getChargingOrderStatistics(params: ChargingOrderQueryParams) {
  return axios.get<ChargingOrderStatistics>('/api/order/statistic', { params });
}

/**
 * 充电桩列表查询参数
 */
export interface ChargingListParams {
  pageNum: number;
  pageSize: number;
  code?: string;
  name?: string;
  chargingGroupId?: number;
  status?: number;
}

/**
 * 充电桩数据
 */
export interface ChargingData {
  id: number;
  code: string;
  name: string;
  socketCount: number;
  chargingGroupId?: number;
  chargingGroupName?: string;
  status: number; // 0: 离线, 1: 在线
}

/**
 * 获取充电桩列表
 */
export function getChargingList(params: ChargingListParams) {
  return axios.get<Result<PageResult<ChargingData>>>('/api/charging/list', {
    params,
  });
}

/**
 * 新增充电桩
 */
export function addCharging(data: Omit<ChargingData, 'id'>) {
  return axios.post<Result>('/api/charging', data);
}

/**
 * 更新充电桩
 */
export function updateCharging(data: ChargingData) {
  return axios.put<Result>('/api/charging', data);
}

/**
 * 删除充电桩
 */
export function deleteCharging(id: number) {
  return axios.delete<Result>(`/api/charging/${id}`);
}

/**
 * 停止充电
 */
export function stopCharging(thirdPartyOrderNo: string | undefined) {
  if (!thirdPartyOrderNo) {
    return Promise.reject(new Error('订单号不能为空'));
  }
  return request.post<string>('/api/order/endCharge', {
    thirdPartyOrderNo,
  });
}

/**
 * 批量导入充电桩
 */
export function importChargingPiles(
  data: Array<{
    code: string;
    name: string;
    socketCount: number;
    chargingGroupId?: number;
  }>
) {
  return axios.post<Result<void>>('/api/charging/import', data);
}

/**
 * 订单退款
 */
export function refundOrder(
  thirdPartyOrderNo: string,
  refundAmount: string,
  reason?: string
) {
  return request.post<string>('/api/order/refund', {
    thirdPartyOrderNo,
    refundAmount,
    reason,
  });
}
